import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';

import { DICT_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';

import { $t } from '#/locales';
import { getRangePickerDefaultProps } from '#/utils';

/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'id',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'name',
      label: $t('views.infra.fileConfig.name'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.infra.fileConfig.namePlaceholder'),
      },
      rules: 'required',
    },
    {
      fieldName: 'storage',
      label: $t('views.infra.fileConfig.storage'),
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.INFRA_FILE_STORAGE, 'number'),
        placeholder: $t('views.infra.fileConfig.storagePlaceholder'),
      },
      rules: 'required',
      dependencies: {
        triggerFields: ['id'],
        show: (formValues) => !formValues.id,
      },
    },
    {
      fieldName: 'remark',
      label: $t('common.remark'),
      component: 'Textarea',
      componentProps: {
        placeholder: $t('common.remarkPlaceholder'),
      },
    },
    // DB / Local / FTP / SFTP
    {
      fieldName: 'config.basePath',
      label: $t('views.infra.fileConfig.config.basePath'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.infra.fileConfig.config.basePathPlaceholder'),
      },
      rules: 'required',
      dependencies: {
        triggerFields: ['storage'],
        show: (formValues) =>
          formValues.storage >= 10 && formValues.storage <= 12,
      },
    },
    {
      fieldName: 'config.host',
      label: $t('views.infra.fileConfig.config.host'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.infra.fileConfig.config.hostPlaceholder'),
      },
      rules: 'required',
      dependencies: {
        triggerFields: ['storage'],
        show: (formValues) =>
          formValues.storage >= 11 && formValues.storage <= 12,
      },
    },
    {
      fieldName: 'config.port',
      label: $t('views.infra.fileConfig.config.port'),
      component: 'InputNumber',
      componentProps: {
        min: 0,
        placeholder: $t('views.infra.fileConfig.config.portPlaceholder'),
      },
      rules: 'required',
      dependencies: {
        triggerFields: ['storage'],
        show: (formValues) =>
          formValues.storage >= 11 && formValues.storage <= 12,
      },
    },
    {
      fieldName: 'config.username',
      label: $t('views.infra.fileConfig.config.username'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.infra.fileConfig.config.usernamePlaceholder'),
      },
      rules: 'required',
      dependencies: {
        triggerFields: ['storage'],
        show: (formValues) =>
          formValues.storage >= 11 && formValues.storage <= 12,
      },
    },
    {
      fieldName: 'config.password',
      label: $t('views.infra.fileConfig.config.password'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.infra.fileConfig.config.passwordPlaceholder'),
      },
      rules: 'required',
      dependencies: {
        triggerFields: ['storage'],
        show: (formValues) =>
          formValues.storage >= 11 && formValues.storage <= 12,
      },
    },
    {
      fieldName: 'config.mode',
      label: $t('views.infra.fileConfig.config.mode'),
      component: 'RadioGroup',
      componentProps: {
        options: [
          { label: $t('views.infra.fileConfig.config.modeActive'), value: 'Active' },
          { label: $t('views.infra.fileConfig.config.modePassive'), value: 'Passive' },
        ],
        buttonStyle: 'solid',
        optionType: 'button',
      },
      rules: 'required',
      dependencies: {
        triggerFields: ['storage'],
        show: (formValues) => formValues.storage === 11,
      },
    },
    // S3
    {
      fieldName: 'config.endpoint',
      label: $t('views.infra.fileConfig.config.endpoint'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.infra.fileConfig.config.endpointPlaceholder'),
      },
      rules: 'required',
      dependencies: {
        triggerFields: ['storage'],
        show: (formValues) => formValues.storage === 20,
      },
    },
    {
      fieldName: 'config.bucket',
      label: $t('views.infra.fileConfig.config.bucket'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.infra.fileConfig.config.bucketPlaceholder'),
      },
      rules: 'required',
      dependencies: {
        triggerFields: ['storage'],
        show: (formValues) => formValues.storage === 20,
      },
    },
    {
      fieldName: 'config.accessKey',
      label: $t('views.infra.fileConfig.config.accessKey'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.infra.fileConfig.config.accessKeyPlaceholder'),
      },
      rules: 'required',
      dependencies: {
        triggerFields: ['storage'],
        show: (formValues) => formValues.storage === 20,
      },
    },
    {
      fieldName: 'config.accessSecret',
      label: $t('views.infra.fileConfig.config.accessSecret'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.infra.fileConfig.config.accessSecretPlaceholder'),
      },
      rules: 'required',
      dependencies: {
        triggerFields: ['storage'],
        show: (formValues) => formValues.storage === 20,
      },
    },
    {
      fieldName: 'config.enablePathStyleAccess',
      label: $t('views.infra.fileConfig.config.enablePathStyleAccess'),
      component: 'RadioGroup',
      componentProps: {
        options: [
          { label: $t('common.enabled'), value: true },
          { label: $t('common.disabled'), value: false },
        ],
        buttonStyle: 'solid',
        optionType: 'button',
      },
      rules: 'required',
      dependencies: {
        triggerFields: ['storage'],
        show: (formValues) => formValues.storage === 20,
      },
      defaultValue: false,
    },
    {
      fieldName: 'config.enablePublicAccess',
      label: $t('views.infra.fileConfig.config.enablePublicAccess'),
      component: 'RadioGroup',
      componentProps: {
        options: [
          { label: $t('views.infra.fileConfig.config.publicAccess'), value: true },
          { label: $t('views.infra.fileConfig.config.privateAccess'), value: false },
        ],
        buttonStyle: 'solid',
        optionType: 'button',
      },
      rules: 'required',
      dependencies: {
        triggerFields: ['storage'],
        show: (formValues) => formValues.storage === 20,
      },
      defaultValue: false,
    },
    // 通用
    {
      fieldName: 'config.domain',
      label: $t('views.infra.fileConfig.config.domain'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.infra.fileConfig.config.domainPlaceholder'),
      },
      rules: 'required',
      dependencies: {
        triggerFields: ['storage'],
        show: (formValues) => !!formValues.storage,
      },
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'name',
      label: $t('views.infra.fileConfig.name'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.infra.fileConfig.namePlaceholder'),
        allowClear: true,
      },
    },
    {
      fieldName: 'storage',
      label: $t('views.infra.fileConfig.storage'),
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.INFRA_FILE_STORAGE, 'number'),
        placeholder: $t('views.infra.fileConfig.storagePlaceholder'),
        allowClear: true,
      },
    },
    {
      fieldName: 'createTime',
      label: $t('common.createTime'),
      component: 'RangePicker',
      componentProps: {
        ...getRangePickerDefaultProps(),
        allowClear: true,
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions['columns'] {
  return [
    { type: 'checkbox', width: 40 },
    {
      field: 'id',
      title: $t('views.common.id'),
      minWidth: 100,
    },
    {
      field: 'name',
      title: $t('views.infra.fileConfig.name'),
      minWidth: 120,
    },
    {
      field: 'storage',
      title: $t('views.infra.fileConfig.storage'),
      minWidth: 100,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.INFRA_FILE_STORAGE },
      },
    },
    {
      field: 'remark',
      title: $t('common.remark'),
      minWidth: 150,
    },
    {
      field: 'master',
      title: $t('views.infra.fileConfig.master'),
      minWidth: 100,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.INFRA_BOOLEAN_STRING },
      },
    },
    {
      field: 'createTime',
      title: $t('common.createTime'),
      minWidth: 180,
      formatter: 'formatDateTime',
    },
    {
      title: $t('common.action'),
      width: 240,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
